import Sequelize from 'sequelize';
import baseDao from '../lib/baseDao.js'; // 数据库操作
import User from './user.js'

const Modal = baseDao.sequelize.define('t_message', {
  // 属性
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true, // 主键
    autoIncrement: true, // 自增
    allowNull: true,
    validate: {
      len: [1, 50]
    }
  },
  title: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 100]
    },
    comment: '消息标题'
  },
  content: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 500]
    },
    comment: '消息详情'
  },
  senderId: {
    type: Sequelize.INTEGER,
    allowNull: true,
    validate: {
      len: [1, 50]
    },
    comment: '消息发送者ID'
  },
  getterId: {
    type: Sequelize.INTEGER,
    allowNull: true,
    validate: {
      len: [1, 50]
    },
    comment: '消息接收者ID'
  },
  type: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 10]
    },
    comment: '消息类型'
  },
  href: {
    type: Sequelize.STRING,
    allowNull: true,
    validate: {
      len: [0, 50]
    },
    comment: '消息处理跳转地址'
  },
  status: {
    type: Sequelize.INTEGER,
    allowNull: true,
    validate: {
      len: [0, 10]
    },
    defaultValue: 1,
    comment: '消息状态，1生效，2结束'
  },
  star: {
    type: Sequelize.INTEGER,
    allowNull: true,
    validate: {
      len: [0, 2]
    },
    defaultValue: 0,
    comment: '是否标星，0否，1是'
  },
  read: {
    type: Sequelize.INTEGER,
    allowNull: true,
    validate: {
      len: [0, 2]
    },
    defaultValue: 0,
    comment: '是否已读，0否，1是'
  },
}, {
    // 参数
  });

Modal.belongsTo(User.Modal, { foreignKey: 'getterId', targetKey: 'id', as: 'getter' });
Modal.belongsTo(User.Modal, { foreignKey: 'senderId', targetKey: 'id', as: 'sender' });

//向外暴露
export default {
  Modal,
}